package com.example.demo.register;

import com.example.demo.constants.DemoConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class SendCodeServiceImpl implements SendCodeService {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    JavaMailSenderImpl sender;

    @Override
    public void send(String mail) {
        validation(mail);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("验证码");
        String code = String.valueOf(new Random().nextInt(899999) + 100000);
        message.setText("您的验证码是" + code + "，三分钟内有效");
        message.setTo(mail);
        message.setFrom("dengzhewen0416@outlook.com");
        Properties properties = sender.getJavaMailProperties();
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.timeout", "25000");
        properties.put("mail.smtp.starttls.enable", "true");
        sender.send(message);
        redisTemplate.opsForValue().set(DemoConstants.REDIS_KEY_CODE_EMAIL + mail, code, 3, TimeUnit.MINUTES);
    }

    private void validation(String mail) {

    }
}
